/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import entity.Groups;

import entity.Role;
import entity.Users;
import facade.GroupsFacadeLocal;
import facade.RoleFacadeLocal;
import facade.UsersFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tatyana
 */
@WebServlet(name = "UserView", urlPatterns = {"/userView"})
public class UserView extends HttpServlet {

    @EJB
    private RoleFacadeLocal roleFacade;
    @EJB
    private GroupsFacadeLocal groupsFacade;
    @EJB
    private UsersFacadeLocal usersFacade;
    protected String sortBy = null;
    protected String keyword = null;
    private String placeToGo = "/index_1.jsp";

    public void init(ServletConfig servletConfig) throws ServletException {
        this.sortBy = servletConfig.getInitParameter("SortBy");
    }

    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {

        int page = 1;
        int recordsPerPage = 5;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        List<Users> users = null;
        if (request.getParameter("keyword") != null) {
  
            keyword = request.getParameter("keyword");
            users = usersFacade.findByKeyword((page - 1) * recordsPerPage, recordsPerPage, keyword);

        } else {

            users = usersFacade.findRange((page - 1) * recordsPerPage, recordsPerPage);
        }
        int noOfRecords = usersFacade.count();
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
        request.setAttribute("users", users);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("currentPage", page);

     //   Collection<Role> user_roles = usersFacade.findRoleByUser(11);
        Collection<Groups> user_groups = usersFacade.findGroupByUser(11);
    

//        HashMap<Integer,Groups> user_in_group =new HashMap<Integer,Groups>();
//        
//        for(Users u: users)
//            for(Groups g: u.getGroupsCollection()){
//            user_in_group.put(u.getIdUser(),g);
//        }





        request.setAttribute("groups", user_groups);
  //      request.setAttribute("roles", user_roles);

        RequestDispatcher view = request.getRequestDispatcher("index_1.jsp");
        view.forward(request, response);
    }

    /*
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//       response.setContentType("text/html;charset=UTF-8");
//       PrintWriter out = response.getWriter();
//      try {
//           
//            int page = 1;
//            int recordsPerPage = 3;
//            if (request.getParameter("page") != null) {
//                page = Integer.parseInt(request.getParameter("page"));
//            }
//            //int[] a ={10 * page + 1, 10 * page + 10};
//            List<Users> users;
//            if (request.getParameter("sort") != null) {
//                sortBy = request.getParameter("sort");
//               // response.sendRedirect(placeToGo);
//            }
//
//            //users = usersFacade.findRangeSortBy(a, sortBy);
//            users = usersFacade.findRange((page-1)*recordsPerPage,recordsPerPage);
//             int noOfRecords = usersFacade.count();
//              int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
//            Collection<Groups> user_group = new ArrayList<Groups>();
//            
//            for(Users user:users){
//                
//                user_group=user.getGroupsCollection();
//            
//            }
//
//           // HashSet<Groups> user_group = 
//            List<Role> user_roles = roleFacade.findAll();
//            List<Groups> user_groups = groupsFacade.findAll();
//            //List<Groups> user_groups=users.getGroupsCollection();
//            HttpSession session = request.getSession();
//            session.setAttribute("users", users);
//            session.setAttribute("groups", user_groups); 
//            session.setAttribute("roles", user_roles);  
//            session.setAttribute("count", usersFacade.count());
//              
//            session.setAttribute("noOfPages", noOfPages);
//            session.setAttribute("currentPage", page);
//            RequestDispatcher dispatcher;
//            dispatcher = getServletContext().getRequestDispatcher("index_1.jsp");
//            dispatcher.forward(request, response);
//
//            /* TODO output your page here. You may use following sample code. */
//
//        } finally {
//              
//            out.close();
//        }
//    }
    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
